/*******************************************************************************
 * Copyright (c) 2002 Boris Pruessmann and others. All rights reserved. This
 * program and the accompanying materials are made available under the terms of
 * the Common Public License v0.5 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors: Boris Pruessmann - Initial implementation.
 ******************************************************************************/
package net.sourceforge.perforce.ui.internal;

import net.sourceforge.perforce.core.resources.RemoteFile;

import org.eclipse.core.resources.IStorage;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.IPersistableElement;
import org.eclipse.ui.IStorageEditorInput;
import org.eclipse.ui.model.IWorkbenchAdapter;

/**
 * @version $Revision: 488 $
 * @author <a href="mailto:bpruessmann@users.sourceforge.net">Boris Pr??mann</a>
 */
public class RemoteFileEditorInput implements IWorkbenchAdapter, IStorageEditorInput {
  // ---------------------------------------------------------------------------
  // Private Variables

  private RemoteFile file;
  private IStorage storage;

  // -------------------------------------------------------------------------
  // Public Constructors

  public RemoteFileEditorInput(RemoteFile file) {
    this.file = file;
  }

  //----------------------------------------------------------------------------
  // -- Public Methods

  public Object[] getChildren(Object o) {
    return new Object[0];
  }

  public ImageDescriptor getImageDescriptor(Object object) {
    return getImageDescriptor();
  }

  public String getLabel(Object o) {
    return file.getDepotFilename();
  }

  public Object getParent(Object o) {
    return null;
  }

  @SuppressWarnings("unused")
  // CoreException
  public IStorage getStorage() throws CoreException {
    if (storage == null) {
      storage = new RemoteFileStorage(file);
    }

    return storage;
  }

  public boolean exists() {
    return true;
  }

  public ImageDescriptor getImageDescriptor() {
    IWorkbenchAdapter adapter = (IWorkbenchAdapter) file.getAdapter(IWorkbenchAdapter.class);
    return (adapter == null) ? null : adapter.getImageDescriptor(file);
  }

  public String getName() {
    return file.getName() + file.getRevision();
  }

  public IPersistableElement getPersistable() {
    return null;
  }

  public String getToolTipText() {
    return file.getDepotFilename();
  }

  @SuppressWarnings("unchecked")
  // adapter
  public Object getAdapter(Class adapter) {
    if (adapter == IWorkbenchAdapter.class) {
      return this;
    }

    return null;
  }

  @Override
  public boolean equals(Object o) {
    if (!(o instanceof RemoteFileEditorInput)) {
      return false;
    }

    RemoteFileEditorInput input = (RemoteFileEditorInput) o;
    return file.equals(input.file);
  }
}
